package droid.training;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataHelper {
	private Context context;
	private SQLiteDatabase db;
	private static final String DATABASE_NAME = "training.db";
	private static final int DATABASE_VERSION = 1;
	private SQLiteStatement insertStmt;
	private static final String TABLE_NAME1 = "sport";
	private static final String TABLE_NAME2 = "measure";
	private static final String INSERT_sport= "insert into "+ TABLE_NAME1 +" (datum, tev, tev_egyeb, megj, ido, ar) values (?,?,?,?,?,?)";
	private static final String INSERT_measure= "insert into "+ TABLE_NAME2 +" (datum, tomeg, derek, csipo, blab, jlab) values (?,?,?,?,?,?)";

	public DataHelper(Context context) {
		this.context = context;
		OpenHelper openHelper = new OpenHelper(this.context);
		this.db = openHelper.getWritableDatabase();
	}
	public long insert_sport(String datum, String tev, String tev_egyeb, String megj, int ido, int ar){
		this.insertStmt = this.db.compileStatement(INSERT_sport);
		this.insertStmt.bindString(1, datum);
		this.insertStmt.bindString(2, tev);
		this.insertStmt.bindString(3, tev_egyeb);
		this.insertStmt.bindString(4, megj);
		this.insertStmt.bindLong(5, ido);
		this.insertStmt.bindLong(6, ar);
    	return this.insertStmt.executeInsert();
    }

	public long insert_measure(String datum, int tomeg, int derek, int csipo, int blab, int jlab){
		this.insertStmt = this.db.compileStatement(INSERT_measure);
		this.insertStmt.bindString(1, datum);
		this.insertStmt.bindLong(2, tomeg);
		this.insertStmt.bindLong(3, derek);
		this.insertStmt.bindLong(4, csipo);
		this.insertStmt.bindLong(5, blab);
		this.insertStmt.bindLong(6, jlab);
    	return this.insertStmt.executeInsert();
	}
	
    public void deleteAll() {
    	this.db.delete(TABLE_NAME1, null, null);
    	this.db.delete(TABLE_NAME2, null, null);
    }

    public List<String> selectAll(String Table) {
    	List<String> list = new ArrayList<String>();
    	Cursor cursor = this.db.query(Table, null, 
    			null, null, null, null, "datum desc");
    	if (cursor.moveToFirst()) {
    		do {
    			list.add(cursor.getString(1));
    			list.add(cursor.getString(2));
    			list.add(cursor.getString(3));
    			list.add(cursor.getString(4));
    			list.add(cursor.getString(5));
    			list.add(cursor.getString(6));
    		} while (cursor.moveToNext());
    	}
    	if (cursor != null && !cursor.isClosed()) {
    		cursor.close();
    	}
    	return list;
    }
    
    public List<String> select(String Table, String ido) {
    	List<String> list = new ArrayList<String>();
    	Cursor cursor = this.db.query(Table, null, 
    			ido, null, null, null, "datum desc");
    	if (cursor.moveToFirst()) {
    		do {
    			list.add(cursor.getString(1));
    			list.add(cursor.getString(2));
    			list.add(cursor.getString(3));
    			list.add(cursor.getString(4));
    			list.add(cursor.getString(5));
    			list.add(cursor.getString(6));
    		} while (cursor.moveToNext());
    	}
    	if (cursor != null && !cursor.isClosed()) {
    		cursor.close();
    	}
    	return list;
    }
	
    private static class OpenHelper extends SQLiteOpenHelper {    	
    	OpenHelper(Context context) {
    		super(context, DATABASE_NAME, null, DATABASE_VERSION);
    	}
    		 
    	@Override
    	public void onCreate(SQLiteDatabase db) {
    		db.execSQL("CREATE TABLE " + TABLE_NAME1 + 
    				"(id INTEGER PRIMARY KEY, datum TEXT, tev TEXT, tev_egyeb TEXT, megj TEXT, ido INTEGER, ar INTEGER)");
    		db.execSQL("CREATE TABLE " + TABLE_NAME2 + 
    				"(id INTEGER PRIMARY KEY, datum TEXT, tomeg INTEGER, derek INTEGER, csipo INTEGER, blab INTEGER, jlab INTEGER)");
    	}

    	@Override
    	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    		Log.w("Example", "Upgrading database, this will drop tables and recreate.");
    		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME1);
    		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
    		onCreate(db);
    	}
    }
}
